package com.spring.sunflower.anchor;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.VideoPlayBean;
import com.spring.sunflower.common.CommonContract;
import com.spring.sunflower.common.VideoPlayActivity;
import com.spring.sunflower.common.VideoPlayPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/6/24<br/>
 * <p>
 * Describe:<br/>
 */
public class VideoShowActivity extends BaseActivity<VideoPlayPresenter> implements CommonContract.VideoPlayView {
    private RecyclerView recyclerView;
    private VideoShowAdapter mAdapter;
    private List<VideoPlayBean.DatasBean> mData = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private int pageIndex = 1;
    private String otherID;

    @Override
    public void initView() {
        Intent intent = getIntent();
        otherID = intent.getStringExtra("otherId");
        tvTitle.setText("视频秀");
        recyclerView = findViewById(R.id.rvRecyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new VideoShowAdapter(R.layout.adapter_item_video_show, mData);
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getVideoList(otherID, String.valueOf(++pageIndex));
            }
        });
        mPresenter.getVideoList(otherID, String.valueOf(pageIndex));

        mAdapter.addChildClickViewIds(R.id.ivPlay);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent(VideoShowActivity.this, VideoPlayActivity.class);
                intent.putExtra("currentIndex", position);
                intent.putExtra("otherId", otherID);
                startActivity(intent);
            }
        });

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_video_show;
    }

    @Override
    public VideoPlayPresenter setPresenter() {
        return new VideoPlayPresenter(this);
    }

    @Override
    public void showVideoList(List<VideoPlayBean.DatasBean> data) {
        if (pageIndex == 1) {
            mAdapter.setList(data);
        } else {
            mAdapter.addData(data);
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }
        if (mData.size() <= 4) {
            refreshLayout.setEnableLoadMore(false);
        } else {
            refreshLayout.setEnableLoadMore(true);
        }
    }
}
